/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t rx_1_data[30];
uint8_t rx_1_flag;
uint8_t rx_1_num;
uint8_t rx_1_buf[1];
uint8_t rx_4_data[50];
uint8_t rx_4_flag;
uint8_t rx_4_num;
uint8_t rx_4_buf[1];
uint8_t rx_5_data[30];
uint8_t rx_5_flag;
uint8_t rx_5_num;
uint8_t rx_5_buf[1];


uint8_t speed_updata_flag;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */




/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_TIM8_Init();
  MX_UART4_Init();
  MX_UART5_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	//TIM Start
	HAL_TIM_Base_Start(&htim8);
	HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_4);
	//Serial receive start
	HAL_UART_Receive_IT(&huart4, (uint8_t*)rx_4_buf, 1); 
	HAL_UART_Receive_IT(&huart5, (uint8_t*)rx_5_buf, 1); 
//	Print(&huart4,"1234\r\n");

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  Print(&huart5,"1234\r\n");
  int temp=1,flag=1;
//  float wa,wd,sa,sd;
  float speed,target_pitch,target_yaw,target_roll;
  //Control_Init();
  
	while(1)
	{
		if(rx_4_flag)
		{
			rx_4_flag = 0;
			sscanf((char*)rx_4_data,"%f,%f,%f,%f\r\n",&speed,&target_yaw,&target_pitch,&target_roll);
			Print(&huart4,"speed1:%.1f,yaw:%.1f,pitch:%.1f,roll:%.1f\r\n", speed, target_yaw, target_pitch, target_roll);
			Motor_Set(speed,speed,speed,speed);
			PID_angle_target_set(target_pitch,target_roll,target_yaw);
			wa_base = speed;
			wd_base = speed;
			sa_base = speed;
			sd_base = speed;
			if(speed >= 1005 && speed < 1100)
			{
				break;
			}
		}
	}

  //control drone
  while (1)
  {
//	  if(temp==2000)  {  flag=-1; }
//	  else if(temp==0){  flag=1;  }
//	  temp+=flag;
//	  __HAL_TIM_SetCompare(&htim8,TIM_CHANNEL_1,temp);
//	  __HAL_TIM_SetCompare(&htim8,TIM_CHANNEL_2,temp);
//	  __HAL_TIM_SetCompare(&htim8,TIM_CHANNEL_3,temp);
//	  __HAL_TIM_SetCompare(&htim8,TIM_CHANNEL_4,temp);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  if(rx_4_flag)
	  {
		  rx_4_flag = 0;
		  sscanf((char*)rx_4_data,"%f,%f,%f,%f\r\n",&speed,&target_yaw,&target_roll,&target_pitch);
		  //Print(&huart4,"speed2:%.1f,yaw:%.1f,pitch:%.1f,roll:%.1f\r\n", speed, target_yaw, target_pitch, target_roll);
		  PID_angle_target_set(target_pitch+2,target_roll-5,target_yaw);
		  wa_base = speed;
		  wd_base = speed;
		  sa_base = speed;
		  sd_base = speed;
		  //Motor_Set(wa,wd,sa,sd);
		  if(speed < 1300)
		  {
			  PID_Param_LowSet();
		  }
		  else if(speed > 1300)
		  {
			  //PID_Clear();
			  PID_Param_HighSet();
		  }
		  else
		  {
			  PID_Param_HighSet();
		  } 
	  }
	  if(speed < 1010)
	  {
		  Motor_Set(speed,speed,speed,speed);
		  continue;
	  }else if(fabs(pitch) > 18 || fabs(roll) > 18)
	  {
		  speed = 1001;
		  wa_base = speed;
		  wd_base = speed;
		  sa_base = speed;
		  sd_base = speed;
		  Motor_Set(speed,speed,speed,speed);
	  }
	  if(rx_5_flag)
	  {
		  if(rx_5_flag == 0x53)
		  {
			  angle_update();
			  Control_Angle();
		  }
		  else if(rx_5_flag == 0x52)
		  {
			  gyro_update();
			  Control_Gyro();
			  //Print(&huart4,"%.0f,%.0f,%.0f\r\n",gyrox,gyroy,gyroz);
		  }
		  else if(rx_5_flag == 0x51)
		  {
			  acc_update();
		  }
		  rx_5_flag = 0;
		  
//		  if(temp == 100)
//		  {
//				Print(&huart4,"%.2f,%.2f,%.2f\r\n",pitch,roll,yaw);
//				temp = 0;
//		  }
//		  temp++;
	 }
	  
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	static uint8_t state_1 = 0;
	static uint8_t state_4 = 0;
	static uint8_t state_5 = 0;
  if (huart->Instance == USART1)
  {
	 //���ݴ���begin
    if (rx_1_buf[0] == '@' && state_1 == 0)
    {
      state_1 = 1;
    }
	else if (rx_1_buf[0] == '\n')
    {
      state_1 = 0;
		rx_1_flag = 1;
		rx_1_data[rx_1_num++] = '\n';
		rx_1_data[rx_1_num] = '\0';
		
    }
	else if(state_1 == 1)
	{
		rx_1_data[rx_1_num++] = rx_1_buf[0];
	}
	//���ݴ���end
    HAL_UART_Receive_IT(&huart1, (uint8_t*)rx_1_buf, 1);
  }
  
  
  if (huart->Instance == UART5) // ???????????? USART1
  {
//	  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
//	  HAL_Delay(10);
//	  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
//	  HAL_Delay(10);
    rx_5_flag = JY_push(rx_5_buf[0]);
    HAL_UART_Receive_IT(&huart5, (uint8_t*)rx_5_buf, 1); // ???? UART ????
  }
  
  if (huart->Instance == UART4)
  {
	 //
    if (rx_4_buf[0] == '@' && state_4 == 0)
    {
      state_4 = 1;
    }
	else if (rx_4_buf[0] == '\n')
    {
		state_4 = 0;
		rx_4_flag = 1;
		rx_4_data[rx_4_num++] = '\n';
		rx_4_data[rx_4_num] = '\0';
		rx_4_num = 0;
		
    }
	else if(state_4 == 1)
	{
		rx_4_data[rx_4_num++] = rx_4_buf[0];
	}
	//
    HAL_UART_Receive_IT(&huart4, (uint8_t*)rx_4_buf, 1);
  }
  
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
